﻿using System;
using System.Collections.Generic;
using System.Text;
using __Able.Network.Utils;
using __Able.Core.Utilities;

namespace CCU.Packet
{
    public class KeepAlivePacket : __Able.Network.Packet
    {
        public const string module = "base";
        public static byte[] version = { 1, 0, 0, 0 };
        public const ushort token = 0x0001;
        public const byte PACKET_TAIL = 0xff;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="user"></param>
        public KeepAlivePacket(ByteBuffer buf, User user)
            : base(buf, user) { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <param name="id"></param>
        public KeepAlivePacket(User user, byte[] id, byte sequence=0)
            : base(module, version, token, sequence, id, user)
        {
            _buf.Initialize();
            //保存当前pos
            int pos = _buf.Position;
            // 填充头部
            PutHead(_buf);
            // 计算包头的长度
            this.HeaderLength = _buf.Position - pos;
            // 填充包体
            PutBody(_buf);
            // 填充尾部
            PutTail(_buf);

            // 回填包头长度
            _buf.PutUShort(pos, (ushort)this.HeaderLength);

            // 加密包 => [包头长度][包头][包体][包尾]
            byte[] enc = Encrypt(_buf.ToByteArray());
            _buf.Initialize();
            // 加密内容写入最终buf
            _buf.PutUShort((ushort)enc.Length);
            _buf.Put(enc);
        }
        /// <summary>
        /// 包头：[序号][包ID][版本号][指令标记] [模块名]
        /// </summary>
        /// <param name="buf"></param>
        protected override void PutHead(ByteBuffer buf)
        {
            // 序号(0-255)
            buf.Put(this.Sequence);
            // 包ID(byte[16])
            buf.Put(this.ID);
            // 版本号(byte[4])
            buf.Put(version);
            // 指令标记(byte[2])
            buf.PutUShort(token);
            // 模块名
            buf.Put(Util.GetBytes(module));
        }
        protected override void PutBody(ByteBuffer buf)
        {
            this.BodyDecrypted = Util.GetBytes(DNTRequest.GetIP());
            buf.Put(this.BodyDecrypted);
        }
        protected override void PutTail(ByteBuffer buf)
        {
            buf.Put(PACKET_TAIL);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="buf"></param>
        protected override void ParseHeader(ByteBuffer buf)
        {
            // 获得包头长度(2)
            this.HeaderLength = buf.GetUShort();
            int pos = buf.Position;
            // 序号(0-255)
            this.Sequence = buf.Get();
            // 包ID(byte[16])
            this.ID = buf.GetByteArray(16);
            // 版本号(byte[4])
            this.Version = buf.GetByteArray(4);
            // 指令标记(byte[2])
            this.Token = buf.GetUShort();
            // 模块名
            this.Module = Util.GetString(buf.GetByteArray(pos + this.HeaderLength - buf.Position));
        }
        /// <summary>
        /// 解析包体
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        protected override byte[] ParseBody(ByteBuffer buf)
        {
            this.BodyDecrypted = buf.GetByteArray(this.PacketLength - this.HeaderLength - 3);
            return this.BodyDecrypted;
        }
        /// <summary>
        /// 解析包尾
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        protected override byte ParseTail(ByteBuffer buf)
        {
            byte b = buf.Get();
            if (b != PACKET_TAIL)
                throw new Exception("不匹配的包尾。");
            return b;
        }
    }
}
